//
//  KY1011AdvertView.m
//  KYMobClick
//
//  Created by 01 on 16/11/8.
//  Copyright © 2016年 Czj. All rights reserved.
//

#import "KY1011AdvertView.h"

@interface KY1011AdvertView ()
@property (nonatomic, strong) UIImageView       *imgView1;
@property (nonatomic, strong) UIImageView       *imgView2;
@property (nonatomic, strong) UIImageView       *imgView3;
@property (nonatomic, strong) UILabel           *titleLabel;
@property (nonatomic, strong) NSMutableDictionary      *dict;
@end

@implementation KY1011AdvertView

#pragma mark - Init
- (instancetype)init {
    self = [super init];
    if (self) {
        _titleLabel = [[UILabel alloc] init];
        _imgView1 = [[UIImageView alloc] init];
        _imgView2 = [[UIImageView alloc] init];
        _imgView3 = [[UIImageView alloc] init];
        
        [self addSubview:_titleLabel];
        [self addSubview:_imgView1];
        [self addSubview:_imgView2];
        [self addSubview:_imgView3];
        
        [self createTap];
    }
    return self;
}

#pragma mark - Public methods
/**
 接收数据填充视图
 */
- (void) createContent:(NSDictionary *)dataDic blockHeight:(void (^)(NSInteger))viewHeight {
    self.dict = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    NSString *pathDocument = [KYSystemInfo getSaveAdvertImagePath];
    NSString *imgUrl;
    NSString *imgUrl1;
    NSString *imgUrl2;
    NSString *text;
    NSString *leftImagePath;
    NSString *middleImagePath;
    NSString *rightImagePath;
    if (dataDic && dataDic[@"data"]) {
        imgUrl = dataDic[@"data"][@"imageurl"];
        imgUrl1 = dataDic[@"data"][@"imageurl1"];
        imgUrl2 = dataDic[@"data"][@"imageurl2"];
        text = dataDic[@"data"][@"headline"];
        leftImagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"left%@.png",self.dict[@"data"][@"adsid"]]];
        middleImagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"middle%@.png",self.dict[@"data"][@"adsid"]]];
        rightImagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"right%@.png",self.dict[@"data"][@"adsid"]]];
    }
    CGFloat titleX = KY_MARGIN;
    CGFloat titleY = KY_MARGIN;
    CGFloat titleW = kDeviceWidth - 2 * KY_MARGIN;
    _titleLabel.font = kSystemFont(kDefaultfontsize);
    _titleLabel.numberOfLines = 0;
    _titleLabel.text =text;
    _titleLabel.frame = CGRectMake(titleX, titleY, titleW, 0);
    [_titleLabel sizeToFit];
    
    CGFloat imgX = _titleLabel.frame.origin.x;
    CGFloat imgY = CGRectGetMaxY(_titleLabel.frame) + KY_MARGIN/2;
    CGFloat imgW = (kDeviceWidth - 3 * KY_MARGIN) / 3;
    CGFloat imgH = kCurrentScreenWidth(64.2);
    _imgView1.frame = CGRectMake(imgX, imgY, imgW, imgH);
    UIImage *leftCacheImage = [UIImage imageWithContentsOfFile:leftImagePath];
    [_imgView1 KYSD_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:leftCacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [UIImagePNGRepresentation(image) writeToFile:leftImagePath atomically:YES];
            CGSize size = image.size;
            CGFloat height= size.height;
            CGFloat width = size.width;
//            KYLog(@"上文下图广告1高度：%f；宽度：%f",height,width);
            CGRect frame = _imgView1.frame;
            frame.size.height = frame.size.width * height / width;
            _imgView1.frame = frame;
        }else{
//            KYLog(@"上文下图广告1加载失败");
        }
    }];

    CGFloat img2X = CGRectGetMaxX(_imgView1.frame) + KY_MARGIN/2;
    _imgView2.frame = CGRectMake(img2X, imgY, imgW, imgH);
    UIImage *middleCacheImage = [UIImage imageWithContentsOfFile:middleImagePath];
    [_imgView2 KYSD_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:imgUrl1] placeholderImage:middleCacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [UIImagePNGRepresentation(image) writeToFile:middleImagePath atomically:YES];
            CGSize size = image.size;
            CGFloat height= size.height;
            CGFloat width = size.width;
//            KYLog(@"上文下图广告2高度：%f；宽度：%f",height,width);
            CGRect frame = _imgView2.frame;
            frame.size.height = frame.size.width * height / width;
            _imgView2.frame = frame;
            [self postPVData];
        }else{
//            KYLog(@"上文下图广告2加载失败");
        }
    }];
    
    CGFloat img3X = CGRectGetMaxX(_imgView2.frame) + KY_MARGIN/2;
    _imgView3.frame = CGRectMake(img3X, imgY, imgW, imgH);
    UIImage *rightCacheImage = [UIImage imageWithContentsOfFile:rightImagePath];
    [_imgView3 KYSD_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:imgUrl2] placeholderImage:rightCacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [UIImagePNGRepresentation(image) writeToFile:rightImagePath atomically:YES];
            CGSize size = image.size;
            CGFloat height= size.height;
            CGFloat width = size.width;
//            KYLog(@"上文下图广告3高度：%f；宽度：%f",height,width);
            CGRect frame = _imgView3.frame;
            frame.size.height = frame.size.width * height / width;
            _imgView3.frame = frame;
            _viewHeight = CGRectGetMaxY(_imgView3.frame) + KY_MARGIN;
            if (viewHeight) viewHeight(_viewHeight);
        }else{
//            KYLog(@"上文下图广告3加载失败");
        }
    }];
}

/**
 创建点击手势
 */
- (void) createTap {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changePage)];
    [self addGestureRecognizer:tap];
}

#pragma mark - Response methods
/**
 统计PV数据
 */
- (void)postPVData {
    //发送统计数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"advertPVData" object:self.dict[@"data"]];
}
/**
 跳转方法
 */
- (void) changePage {
//    [_dict setValue:kOnClick forKey:@"clickStatus"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickView:)]) {
        [self.delegate clickView:self.dict[@"data"]];
    }
}

#pragma mark - Lazy load
- (NSMutableDictionary *)dict {
    if (!_dict) {
        _dict = [[NSMutableDictionary alloc] init];
    }
    return _dict;
}
@end
